package com.taobao.com;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.security.NoSuchAlgorithmException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.taobao.api.internal.util.TaobaoUtils;

public class Verfy_Sign extends HttpServlet {
	public static String topParams="";
	public static String topSession="";
	public static String topSign="";
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		boolean falg=false;
		topParams=request.getParameter("top_parameters");
		topSession=request.getParameter("top_session");
		topSign=request.getParameter("top_sign");
		//验证签名
		//try {
			//签名验证的方法可以通过通过TaobaoUtils.verifyTopResponse()调用验证如下（建议使用）：
		falg=TaobaoUtils.verifyTopResponse(URLDecoder.decode(topParams, "utf-8"), URLDecoder.decode(topSession, "utf-8"), URLDecoder.decode(topSign, "utf-8"), Util.APP_KEY, Util.APP_SERCET);
		//也可以自己写代码如下：
			//falg=Util.verifyTopResponse(URLDecoder.decode(topParams, "utf-8"), URLDecoder.decode(topSession, "utf-8"), URLDecoder.decode(topSign, "utf-8"), Util.APP_KEY, Util.APP_SERCET);
		//} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
		//}
		if(falg==true){//签名验证通过跳转到成功页面解析topParams获取nick等参数，topSession即为调用接口需要的sessionkey（授权码）
			response.sendRedirect("verfySign.jsp?topParams="+topParams+"&topSession="+topSession+"&topSign="+topSign+"");
		}else{
			response.sendRedirect("error.jsp");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void init() throws ServletException {
		// Put your code here
	}

}
